package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.*;
import java.util.*;

import javax.servlet.http.*;

import models.SolutionSubmission;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

@SuppressWarnings("serial")
public class ajxGetExpertRecommServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/xml");
		
		boolean bCustomer = false;
		boolean bLocation = false;
		boolean bProcessarea = false;
		boolean bSolType = false;
		boolean bFinBenfit = false;
		boolean bAchBenfit = false;
		boolean bfromDt = false;
		boolean btoDt = false;
		long solID;
		String solTitle = "";
		String solCustomer = "";
		String solLocation = "";
		String solCode ="";
		
		String cmbCustomer = req.getParameter("cmbCustomer");
		String cmbLocation = req.getParameter("cmbLocation");
		String cmbProcessarea = req.getParameter("cmbProcessarea");
		String cmbSolType = req.getParameter("cmbSolType");
		String cmbFinBenfit = req.getParameter("cmbFinBenfit");
		String cmbAchBenfit = req.getParameter("cmbAchBenfit");
		String fromDt = req.getParameter("fromDt");
		String toDt = req.getParameter("toDt");	
		if (cmbCustomer != null)
		{
			bCustomer = true;
		}
		if (cmbLocation != null)
		{
			bLocation = true;
		}
		if (cmbProcessarea != null)
		{
			bProcessarea = true;
		}
		if (cmbSolType != null)
		{
			bSolType = true;
		}
		if (cmbFinBenfit != null)
		{
			bFinBenfit = true;
		}
		if (cmbAchBenfit != null)
		{
			bAchBenfit = true;
		}
		if (fromDt != null)
		{
			bfromDt = true;
		}
		if (toDt != null)
		{
			btoDt = true;
		}
		
		String strHTML = "";
		String tempStr = "";
		long recommendCount = 0;
		int i = 0;
		boolean ifound = false;
		PrintWriter out = resp.getWriter();

		try {
			
			strHTML = strHTML + "<Table id=\"insured_list\" class=\"tablesorter\"   cellpadding=3 width=\"550px\">";
			strHTML = strHTML + "<thead>";
			strHTML = strHTML + "<tr>";
			strHTML = strHTML + "<th    width=\"100px\"></b></th>";
			strHTML = strHTML + "<th    width=\"100px\"><b>Solution ID</b></th>";
			strHTML = strHTML + "<th   width=\"150px\" align=\"center\"><b>Location</b></th>";
			strHTML = strHTML + "<th   width=\"150px\" align=\"center\"><b>Solution Title</b></th>";
			strHTML = strHTML + "<th  width=\"150px\" align=\"center\"><b>Customer</b></th>";
			strHTML = strHTML + "</tr></thead>";
			strHTML = strHTML + "<tbody> ";

			Objectify ofy = ObjectifyService.begin();
			Query<SolutionSubmission> q = ofy.query(SolutionSubmission.class).filter("hasExpert", true);
			//Query<SolutionSubmission> q = ofy.query(SolutionSubmission.class);
			if (bCustomer)
			{
				q.filter("Customer", cmbCustomer);
			}
			if (bLocation)
			{
				q.filter("Location", cmbLocation);
			}
			if (bProcessarea)
			{
				q.filter("processArea", cmbProcessarea.replace(" and ", "&"));
			}
			if (bSolType)
			{
				q.filter("solType", cmbSolType.toString().trim());
			}
			if (bFinBenfit)
			{
				if (cmbFinBenfit.toString().trim().equals("Hard Savings")) {
					q.filter("hasHardSavings", true);
				} else if (cmbFinBenfit.toString().trim().equals("Cost Avoidance")) {
					q.filter("hasCostAvoidance", true);
				} else if (cmbFinBenfit.toString().trim().equals("Cost Avoidance")) {
					q.filter("hasOppurtunityCost", true);
				}		
			}
			if (bAchBenfit)
			{
				q.filter("achievedTypeDesc", cmbAchBenfit);
			}
			if (bfromDt)
			{
				Date fdate = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.ENGLISH).parse(fromDt + " 00:00:01");
				q.filter("submittedON >=", fdate);
			}
			if (btoDt)
			{
				Date tdate = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss", Locale.ENGLISH).parse(toDt + " 23:59:59");
				q.filter("submittedON <=", tdate);
			}

			i = 0;
			for (SolutionSubmission f : q.fetch()) {
				solID = f.getSolID();
				solCode = f.getSolCode(); //ADDED BY KULPRAPAS SEP 27,2012 - To get the Solution Code instead Solution ID
				solTitle = f.getTitle();
				solLocation = f.getLocation();
				solCustomer = f.getCustomer();
								
				//SEARCH THE RECOMMEND	
				RecommendConnect REC = new RecommendConnect();
				recommendCount = REC.getCount("solID", solID);

				tempStr = "<a href='#' rel='/modules/default/expert-info-display.jsp?sol_id=" + solID + "' id='sticky" + i + "' title='Experts Recommended'>";								
				strHTML = strHTML + "<tr>";
				strHTML = strHTML + "<td width='100px' style='text-align:center'>" + tempStr + recommendCount + "<img src='/img/Thumbicon.gif' /></a> </td>";
				strHTML = strHTML + "<td width='100px'><a href='/modules/default/display-solution.jsp?sol_id=" + solID + "' >" + solCode + "</td>";
				strHTML = strHTML + "<td width='150px'>" + solLocation + "</a></td>";
				strHTML = strHTML + "<td width='150px' align='center'>"+ solTitle +"</td>";
				strHTML = strHTML + "<td width='150px' align='center'>" + solCustomer + "</td>";				
				strHTML = strHTML + "</tr>";
				i++;
				ifound = true;
			}
			strHTML = strHTML + "</tbody> ";
			strHTML = strHTML + "</table>";			
			if (!ifound)
			{
				strHTML = "<div style='font-family:Arial;font-weight:bold;color:black;font-size:25px'><center>There are no data for Expert Recommend Solutions listed</center></div>";
			}			
			strHTML = strHTML + "<div id='countera' style='display:none'>" + i + "</div>";
			out.println(strHTML);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			//out.println("MyFavouriteServlet:Exception" + e.getMessage());
			//e.printStackTrace();
			strHTML = "<div style='font-family:Arial;font-weight:bold;color:black;font-size:25px'><center>" + e.getMessage() + "</center></div>";
			out.println(strHTML);
		}
	}
}
